package com.example.esp32;

import static android.content.ContentValues.TAG;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceManager;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.skydoves.colorpickerview.AlphaTileView;
import com.skydoves.colorpickerview.ColorEnvelope;
import com.skydoves.colorpickerview.ColorPickerView;
import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener;
import com.skydoves.colorpickerview.listeners.ColorListener;
import com.skydoves.colorpickerview.listeners.ColorPickerViewListener;

import org.json.JSONException;
import org.json.JSONObject;

public class rgb extends AppCompatActivity {


    private Button button;
private EditText a;
    private Toolbar toolbar;

//    private AlphaTileView alphaTileView;
private ColorPickerView alphaTileView;
private Button buttonyg;
    private EditText g;
    private EditText b;
private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rgb);
        toolbar();
//        boolean spIsAuth = sharedPreferences.getBoolean("is_auth", false);  // 第一个参数是key，第二个是根据key取不到值时的默认值
//        String spPassword = sharedPreferences.getString("password", "");
        textView=findViewById(R.id.zttext);

        button=findViewById(R.id.button);
        alphaTileView=findViewById(R.id.color);
        buttonyg=findViewById(R.id.button23);
        a=findViewById(R.id.r);
        g=findViewById(R.id.g);
        b=findViewById(R.id.b);
        String R=a.getText().toString();
        String G=g.getText().toString();
        String B=b.getText().toString();
        int w =Integer.parseInt(a.getText().toString());
        int s =Integer.parseInt(g.getText().toString());
        int d =Integer.parseInt(b.getText().toString());
        if (R=="" | G==""  | B=="" ){
            a.setText("0");
            b.setText("0");
            g.setText("0");

        }
        buttonyg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(rgb.this,yg.class);
                startActivity(intent);
            }
        });
       alphaTileView.setColorListener(new ColorEnvelopeListener() {
           @Override
           public void onColorSelected(ColorEnvelope color, boolean fromUser) {

               int[] z=color.getArgb();
               a.setText(String.valueOf(z[1]));
               g.setText(String.valueOf(z[2]));
               b.setText(String.valueOf(z[3]));
               rgb(String.valueOf(z[1]),String.valueOf(z[2]),String.valueOf(z[3]));

               Log.d(TAG, "colo"+":"+z[1]+","+z[2]+","+z[3]);
           }
       });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if(w>255 |s>255|d>255){
                    Toast.makeText(rgb.this,"不不不R,G,B,最大也就255，你想干什么", Toast.LENGTH_SHORT).show();

                }else {
                rgb(R,G,B);}
            }
        });
    }



        private void rgb(String R,String G,String B){
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            String MQTT = sharedPreferences.getString("MQTT", "");
            String RGBAPI = sharedPreferences.getString("RGBAPI", "");
            if(RGBAPI.equals("")){
                Toast.makeText(this, "请检查RGBAPI配置", Toast.LENGTH_SHORT).show();
            }
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    request request =new request();
                    JSONObject jsonObject = new JSONObject();

                    try {
                        jsonObject.put("toicp", MQTT);
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                    try {
                        jsonObject.put("R", R);
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }

                    try {
                        jsonObject.put("G", G);
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                    try {
                        jsonObject.put("B", B);
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }

                    String a;
                    try {
                        a = jsonObject.toString(1);
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                    request requset=new request();
                    String data=a;

                    String resq=requset.post(RGBAPI,data);


//                    String resq=requset.post("http://ipzhangt:8999/rgb",data);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d(TAG, "run: "+data);
                            textView.setText("返回结果："+resq);
//                            Toast.makeText(rgb.this, "返回结果："+resq, Toast.LENGTH_SHORT).show();

                        }
                    });
                }
            }.start();
        }


    public void toolbar() {
        toolbar = findViewById(R.id.toolbar);
        //标题
        toolbar.setTitle("RGB调色");
        toolbar.setBackgroundResource(R.color.sh);

    }

    }

